﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CCL;

namespace Client
{
    public partial class CheckOutForm : System.Windows.Forms.Form
    {
        public CheckOutForm()
        {
            InitializeComponent();
        }

        public CheckOutForm(string directoryAdr)
        {
            InitializeComponent();
            txtDirectory.Text = directoryAdr;
        }

        // 폴더 경로 탐색 후 텍스트창에 경로 입력
        private void btnDirectorySearch_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1 = new FolderBrowserDialog();
            DialogResult res = folderBrowserDialog1.ShowDialog();
            txtDirectory.Text = res.ToString();
        }

        // 체크 아웃 이벤트
        private void btnCheckOut_Click(object sender, EventArgs e)
        {
            int projID = MainForm.projList[cbProject.SelectedIndex].ProjectID;
            int userID = MainForm.userData.UserID;

            CCL.SocketMessage.Request.CheckOutProject packet = new CCL.SocketMessage.Request.CheckOutProject(projID, userID, 0);
            CCL.SocketMessage.Message message = CCL.SocketMessage.Message.MessageRequestBuilder(packet);
            byte[] msg = message.GetByte();

            CCL.SocketMessage.Message checkOutMsg = MainForm.connector.Execute(msg);
            CCL.SocketMessage.Response.FileDownloadReady checkOutResponse = (CCL.SocketMessage.Response.FileDownloadReady)checkOutMsg.Body;

            if (checkOutResponse.Result == CCL.SocketMessage.Constant.Result.Success)
            {
                for (int i = 0; i < checkOutResponse.FileCount; i++)
                {
                    CCL.SocketMessage.Request.FileDownload downPacket = new CCL.SocketMessage.Request.FileDownload(checkOutResponse.PoolID);
                    message = CCL.SocketMessage.Message.MessageRequestBuilder(downPacket);
                    byte[] downMsg = message.GetByte();

                    CCL.SocketMessage.Message downPacketMsg = MainForm.connector.Execute(downMsg);
                    CCL.SocketMessage.Response.FileDownload fileDownloadResponse = (CCL.SocketMessage.Response.FileDownload)downPacketMsg.Body;

                    // 파일 경로 : 루트 + 파일명
                    CCL.Utility.FileUtility.BytesToFile(fileDownloadResponse.FileBytes, txtDirectory.Text + fileDownloadResponse.FileName);

                    progressBar1.Value = ((i + 1) / checkOutResponse.FileCount) * 100;    
                }

                // Text File Create
                    string[] data = { projID.ToString(), MainForm.projList[cbProject.SelectedIndex].HeadRevision.ToString(), System.DateTime.Now.ToString("yyyyMMddHHmmss") };
                    TxtReader.CreateDataFile(txtDirectory.Text);
                    TxtReader.WriteDataFile(txtDirectory.Text, data);
                    TxtReader.WriteSystemFile(projID.ToString(), txtDirectory.Text);MessageBox.Show("Complete Check Out");
                this.Close();
            }
            else
            {
                MessageBox.Show(CCL.Utility.StringUtility.ConvertToStr(checkOutResponse.ErrorMessage));
            }
            
        }

        // 프로젝트 (콤보박스) 에서 데이터를 변경할 경우 발생하는 이벤트
        private void cbProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbProjectName.Text = "프로젝트 명 : " + CCL.Utility.StringUtility.ConvertToStr(MainForm.projList[cbProject.SelectedIndex].ProjectName);
            lbAdminID.Text = "관리자 : " + CCL.Utility.StringUtility.ConvertToStr(MainForm.projList[cbProject.SelectedIndex].ProjectOwner.Email);
        }

        // 체크아웃폼 로드 이벤트
        private void CheckOutForm_Load(object sender, EventArgs e)
        {
            foreach(CCL.TableStructure.Project project in MainForm.projList)
            {
                cbProject.Items.Add(CCL.Utility.StringUtility.ConvertToStr(project.ProjectName));
            }
        }
    }
}
